/*******************************************************************************************
This file is part of iToday.

iToday is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

iToday is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with iToday.  If not, see <http://www.gnu.org/licenses/>.
* ------------------------------------------------------------------------------
*    Utility.cpp - implement tool for debug info print and other common functions
* ------------------------------------------------------------------------------
*    Revision History
*    Date                  Author         Activity ID               Activity Headline
*    2009-06-18            CubeWang          none                     initial
*    2010-02-23            Jake Lin          none                     Added Logger class
*******************************************************************************************/

#include "stdafx.h"
#include "LogUtility.h"

FILE* Logger::m_hLogFile = NULL;
LOG_LEVEL Logger::m_enLogLevel = LOG_TRACE;
std::string Logger::m_strFilePath = "\\Storage Card\\DebugInfo.log";

TCHAR * LogLevelStr[]=
{
    TEXT("TRACE "),
    TEXT("INFO "),
    TEXT("WARNING "),
    TEXT("ERROR "),
    TEXT("CRITICAL "),
    TEXT("FATAL "),
};

Logger& Logger::Instance() 
{
  static Logger oLogger;
  return oLogger;
}

void Logger::SetLogFilePath( const std::string& strFilePath)
{
	m_strFilePath = strFilePath;
	Initialise();
}

void Logger::SetLogLevel( const LOG_LEVEL enLogLevel)
{
	m_enLogLevel = enLogLevel;
}


Logger::Logger()
{
	Initialise();
}

//never use
Logger::~Logger()
{
	Dispose();
}

void Logger::Initialise()
{
	if( m_strFilePath.length() > 0 )
	{
		m_hLogFile = fopen(m_strFilePath.c_str(), "a+");	
	}
}

void Logger::Dispose()
{
	if( NULL != m_hLogFile )
	{
		fflush( m_hLogFile );
		fclose( m_hLogFile );
		m_hLogFile = NULL;
	}
}

void Logger::Log( LOG_LEVEL enLogLevel ,const TCHAR *format, ... )
{
	if( m_enLogLevel > enLogLevel)
	{
		return;
	}
#ifndef DEBUG
	if ( NULL == m_hLogFile )
	{
		return;	
	}
#endif

	TCHAR szBuffer[1024];

	va_list args;
    va_start(args, format);
	vswprintf(szBuffer, format, args);
	va_end(args);

#ifdef DEBUG
	//wprintf(_T("%s"), szBuffer);
	wprintf(_T("%S THR:%8.8x %s\t%s\n"), GetCurrentTime(), GetCurrentThread(), LogLevelStr[enLogLevel], szBuffer);
#else
	//combine time stamp, thread number and log level together.
	if( 0 > fwprintf(m_hLogFile, _T("%S THR:%8.8x %s\t%s\n"), GetCurrentTime(), GetCurrentThread(), LogLevelStr[enLogLevel], szBuffer) )
	{
		Dispose();
	}
	else
	{
		fflush(m_hLogFile);
	}
#endif
	
}



